/**
 * 
 */
package com.carnation.qa.socketserver.common.util;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Nick
 * 
 */
public class MppContext {

	private final Log log = LogFactory.getLog(getClass());
	private static MppContext self;
	private static String tab, newline, dateformat;
	private static int socketserverListenerPort;

	static {
		self = new MppContext();
		init();
	}

	private MppContext() {

	};

	private static void init() {
		Properties prop = null;
		try {
			prop = new Properties();
			InputStream in = MppContext.class.getClassLoader()
					.getResourceAsStream("socketserver.properties");
			prop.load(in);
			if (in != null) {
				in.close();
			}
		} catch (Exception e) {
		}
		tab = getValue(prop, "text.separator.tab", "\t");
		newline = getValue(prop, "text.separator.newline", "\r\n");
		dateformat = getValue(prop, "text.format.date", "yyyy-MM-dd hh:mm:ss");
		socketserverListenerPort = Integer.parseInt(getValue(prop,
				"socketserver.listener.port", "10000"));
	}

	private static String getValue(Properties prop, String key,
			String defaultValue) {
		try {
			return new String(prop.getProperty(key));
		} catch (Exception e) {
			if (self.log.isWarnEnabled()) {
				self.log.warn("Could not find configuration " + "[" + key
						+ "];" + " using defaults:" + defaultValue);
			}
			return defaultValue;
		}
	}

	public static String getTabSeparator() {
		return tab;
	}

	public static String getNewlineSeparator() {
		return newline;
	}

	public static String getDateFormat() {
		return dateformat;
	}

	public static int getInt(String string, int i) {
		return i;
	}

	/**
	 * @return the socketserverListenerPort
	 */
	public static int getSocketserverListenerPort() {
		return socketserverListenerPort;
	}

}
